package guda.house.biz.impl;

import guda.grape.autogen.common.page.BizResult;
import guda.house.biz.BillStatOtherBiz;
import guda.house.biz.beans.vo.StatEstateDiscountVO;
import guda.house.biz.enums.EstateDiscountTypeEnum;
import guda.house.biz.enums.StatInvoiceTypeEnum;
import guda.house.biz.form.stat.StatFeeForm;
import guda.house.biz.query.*;
import guda.house.biz.util.Transform;
import guda.house.biz.util.TransformHelper;
import guda.house.dal.dao.*;
import guda.house.dal.dataobject.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by well on 16/2/27.
 */
public class BillStatOtherBizImpl implements BillStatOtherBiz {

    @Autowired
    private InvoiceLogDAO invoiceLogDAO;
    @Autowired
    private OwnerDAO ownerDAO;
    @Autowired
    private SuiteDAO suiteDAO;
    @Autowired
    private UnitDAO unitDAO;
    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private BillEstateDAO billEstateDAO;

    @Override
    public BizResult statInvoice(StatFeeForm statFeeForm) {
        BizResult bizResult = new BizResult();
        List<Long> suiteIdList = getSuiteId(statFeeForm);
        InvoiceLogQuery invoiceLogQuery = new InvoiceLogQuery();
        invoiceLogQuery.setPageSize(statFeeForm.getPageSize());
        invoiceLogQuery.setPageNo(statFeeForm.getPageNo());
        invoiceLogQuery.setSuiteIdList(suiteIdList);
        if(StatInvoiceTypeEnum.invoice_no.name().equals(statFeeForm.getType())){
           if(StringUtils.hasText(statFeeForm.getInputValue())){
               invoiceLogQuery.setInvoiceNo(statFeeForm.getInputValue());
           }
        }
        List<InvoiceLogDO> invoiceLogDOs = invoiceLogDAO.selectForPage(invoiceLogQuery);
        int i = invoiceLogDAO.countForPage(invoiceLogQuery);
        invoiceLogQuery.setTotalCount(i);
        bizResult.data.put("query", invoiceLogQuery);
        bizResult.data.put("invoiceLogDOList",invoiceLogDOs);
        bizResult.success=true;
        return bizResult;
    }

    @Override
    public BizResult statEstateDiscount(StatFeeForm statFeeForm) {
        BizResult bizResult = new BizResult();
        BillEstateQuery billEstateQuery = new BillEstateQuery();
        billEstateQuery.setPageNo(statFeeForm.getPageNo());
        billEstateQuery.setPageSize(statFeeForm.getPageSize());
        if(EstateDiscountTypeEnum.dichan.name().equals(statFeeForm.getType())){
            billEstateQuery.setDichanDiscount("1");
        }else{
            billEstateQuery.setSelfDiscount("1");
        }
        Date startDate = null;
        Date endDate = null;
        try {
            if(StringUtils.hasText(statFeeForm.getStartDate()) && StringUtils.hasText(statFeeForm.getEndDate())) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
                startDate = simpleDateFormat.parse(statFeeForm.getStartDate());
                simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
                endDate = simpleDateFormat.parse(statFeeForm.getEndDate());
                if (startDate.after(endDate)) {
                    Date temp = startDate;
                    startDate = endDate;
                    endDate = temp;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        billEstateQuery.setStartDate(startDate);
        billEstateQuery.setEndDate(endDate);
        List<BillEstateDO> billEstateDOList = billEstateDAO.selectForPage(billEstateQuery);
        Set<Long> suiteIdSet = TransformHelper.transform2Set(billEstateDOList, new Transform<BillEstateDO, Long>() {
            @Override
            public Long transform(BillEstateDO object) {
                return object.getSuiteId();
            }
        });
        List<Long> suiteIdList = new ArrayList<Long>(suiteIdSet);
        Map<Long,SuiteDO> suiteDOMap = new HashMap<Long, SuiteDO>();
        List<SuiteDO> suiteByIds = suiteDAO.getSuiteByIds(suiteIdList);
        for(SuiteDO suiteDO:suiteByIds){
            suiteDOMap.put(suiteDO.getSuiteId(),suiteDO);
        }

        Map<Long,String> ownerDOMap = new HashMap<Long, String>();
        OwnerQuery ownerQuery = new OwnerQuery();
        ownerQuery.setPageSize(1000);
        ownerQuery.setSuiteIdList(suiteIdList);
        List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
        for (OwnerDO ownerDO:ownerDOs){
            String s = ownerDOMap.get(ownerDO.getSuiteId());
            if(s == null){
                ownerDOMap.put(ownerDO.getSuiteId(),ownerDO.getOwnerName());
            }else{
                ownerDOMap.put(ownerDO.getSuiteId(), s + "," +ownerDO.getOwnerName());
            }
        }

        List<StatEstateDiscountVO> statEstateDiscountVOList = new ArrayList<StatEstateDiscountVO>(billEstateDOList.size());
        for(BillEstateDO billEstateDO:billEstateDOList){
            StatEstateDiscountVO statEstateDiscountVO = new StatEstateDiscountVO();
            statEstateDiscountVO.setOwnerName(ownerDOMap.get(billEstateDO.getSuiteId()));
            statEstateDiscountVO.setSuiteId(billEstateDO.getSuiteId());
            SuiteDO suiteDO = suiteDOMap.get(billEstateDO.getSuiteId());
            if(suiteDO!= null) {
                statEstateDiscountVO.setSuiteName(suiteDO.getSuiteName());
            }
            if(EstateDiscountTypeEnum.dichan.name().equals(statFeeForm.getType())){
                statEstateDiscountVO.setDiscountType(EstateDiscountTypeEnum.dichan.getDesc());
                statEstateDiscountVO.setDiscount(billEstateDO.getDiscountEstate());
            }else{
                statEstateDiscountVO.setDiscountType(EstateDiscountTypeEnum.self.getDesc());
                statEstateDiscountVO.setDiscount(billEstateDO.getDiscountSelf());
            }
            statEstateDiscountVOList.add(statEstateDiscountVO);


        }
        bizResult.data.put("statEstateDiscountVOList",statEstateDiscountVOList);
        int i = billEstateDAO.countForPage(billEstateQuery);
        billEstateQuery.setTotalCount(i);
        bizResult.data.put("query",billEstateQuery);

        bizResult.success=true;
        return bizResult;
    }

    private List<Long> getSuiteId(StatFeeForm statFeeForm){
        if(statFeeForm.getVillageId() == null){
            return null;
        }
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(10000);
        buildingQuery.setVillageId(statFeeForm.getVillageId());
        List<BuildingDO> buildingDOs = buildingDAO.selectForPage(buildingQuery);
        List<Long> buildingIdList = TransformHelper.transform(buildingDOs, new Transform<BuildingDO, Long>() {
            @Override
            public Long transform(BuildingDO object) {
                return object.getBuildingId();
            }
        });
        if (buildingIdList.size() == 0) {
            return null;
        }

        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setPageSize(10000);
        unitQuery.setBuildingIdList(buildingIdList);
        List<UnitDO> unitDOs = unitDAO.selectForPage(unitQuery);
        List<Long> unitIdList = TransformHelper.transform(unitDOs, new Transform<UnitDO, Long>() {
            @Override
            public Long transform(UnitDO object) {
                return object.getUnitId();
            }
        });
        if (unitIdList.size() == 0) {
            return null;
        }

        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setPageSize(10000);
        suiteQuery.setUnitIdList(unitIdList);
        List<SuiteDO> suiteDOs = suiteDAO.selectForPage(suiteQuery);
        List<Long> suiteIdList = TransformHelper.transform(suiteDOs, new Transform<SuiteDO, Long>() {
            @Override
            public Long transform(SuiteDO object) {
                return object.getSuiteId();
            }
        });
        if(StringUtils.isEmpty(statFeeForm.getType())){
            return suiteIdList;
        }
        if(StatInvoiceTypeEnum.all.name().equals(statFeeForm.getType())){
            return suiteIdList;
        }else if(StatInvoiceTypeEnum.owner_name.name().equals(statFeeForm.getType())){
            if(StringUtils.isEmpty(statFeeForm.getInputValue())) {
                return suiteIdList;
            }
            OwnerQuery ownerQuery = new OwnerQuery();
            ownerQuery.setOwnerName(statFeeForm.getInputValue());
            ownerQuery.setSuiteIdList(suiteIdList);
            List<OwnerDO> ownerDOs = ownerDAO.selectForPage(ownerQuery);
            List<Long> result = TransformHelper.transform(ownerDOs, new Transform<OwnerDO, Long>() {
                @Override
                public Long transform(OwnerDO object) {
                    return object.getSuiteId();
                }
            });
            return result;
        }else if(StatInvoiceTypeEnum.suite_no.name().equals(statFeeForm.getType())){
            if(StringUtils.isEmpty(statFeeForm.getInputValue())) {
                return suiteIdList;
            }
            SuiteQuery suiteQuery1 = new SuiteQuery();
            suiteQuery1.setSuiteName(statFeeForm.getInputValue());
            suiteQuery1.setSuiteIdList(suiteIdList);
            List<SuiteDO> suiteDOList = suiteDAO.selectForPage(suiteQuery1);
            List<Long> result = TransformHelper.transform(suiteDOList, new Transform<SuiteDO, Long>() {
                @Override
                public Long transform(SuiteDO object) {
                    return object.getSuiteId();
                }
            });
            return result;
        }
        return suiteIdList;


    }
}
